package duyd.ws.training.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@SuppressWarnings("serial")
@Entity
@Table(name = "CONTACT")
@NamedQueries({
		@NamedQuery(name = "contact.findAll", query = "select c from Contact c"),
		@NamedQuery(name = "contact.findById", query = "select distinct c from Contact c left join fetch c.tels t left join fetch c.hobbies h where c.id=:id"),
		@NamedQuery(name = "contact.findAllWithDetail", query = "select distinct c from Contact c left join fetch c.tels t left join fetch c.hobbies h") })
@XmlRootElement
public class Contact implements Serializable {

	private Long id;

	private String firstName;

	private String lastName;

	private Date birthDate;

	private int version;

	private Set<ContactTelDetail> tels = new HashSet<ContactTelDetail>();

	private Set<Hobby> hobbies = new HashSet<Hobby>();

	@XmlElement
	@Id
	@Column(name = "CONTACT_ID")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@XmlElement
	@Column(name = "FIRST_NAME")
	@Size(min = 3, max = 60)
	@NotNull
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@XmlElement
	@Column(name = "LAST_NAME")
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@XmlElement
	@Column(name = "BIRTH_DATE")
	@Temporal(TemporalType.DATE)
	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	@XmlElement
	@Column(name = "CONTACT_VERSION")
	@Version
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@XmlElement(required = false)
	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "contact")
	public Set<ContactTelDetail> getTels() {
		return tels;
	}

	public void setTels(Set<ContactTelDetail> tels) {
		this.tels = tels;
	}

	@XmlElement(required = false)
	@ManyToMany
	@JoinTable(name = "CONTACT_HOBBY_DETAIL", joinColumns = @JoinColumn(name = "CONTACT_ID"), inverseJoinColumns = @JoinColumn(name = "HOBBY_ID"))
	public Set<Hobby> getHobbies() {
		return hobbies;
	}

	public void setHobbies(Set<Hobby> hobbies) {
		this.hobbies = hobbies;
	}

	public String toString() {
		return "Contact[Id: " + id + ", First name: " + firstName
				+ ", Last name: " + lastName + ", BirthDate: " + birthDate
				+ "]";
	}
}
